package com.wskj.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wskj.common.core.mybatisplus.core.ServicePlusImpl;
import com.wskj.common.core.page.PagePlus;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.utils.common.AssertUtils;
import com.wskj.common.utils.common.PageUtils;
import com.wskj.system.bo.SysNoticeReadAddBo;
import com.wskj.system.bo.SysNoticeReadEditBo;
import com.wskj.system.bo.SysNoticeReadQueryBo;
import com.wskj.system.domain.SysNoticeRead;
import com.wskj.system.mapper.SysNoticeReadMapper;
import com.wskj.system.service.ISysNoticeReadService;
import com.wskj.system.vo.web.SysNoticeReadVo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 通知公告已读Service业务层处理
 *
 * @author wsdz
 * @date 2022-12-14
 */
@Service
public class SysNoticeReadServiceImpl extends ServicePlusImpl<SysNoticeReadMapper, SysNoticeRead> implements ISysNoticeReadService {

    @Override
    public SysNoticeReadVo queryById(Long readId){
        return getVoById(readId, SysNoticeReadVo.class);
    }

    @Override
    public TableDataInfo<SysNoticeReadVo> queryPageList(SysNoticeReadQueryBo bo) {
        PagePlus<SysNoticeRead, SysNoticeReadVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysNoticeReadVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysNoticeReadVo> queryList(SysNoticeReadQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysNoticeReadVo.class);
    }

    private LambdaQueryWrapper<SysNoticeRead> buildQueryWrapper(SysNoticeReadQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysNoticeRead> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getNoticeId() != null, SysNoticeRead::getNoticeId, bo.getNoticeId());
        lqw.eq(bo.getUserId() != null, SysNoticeRead::getUserId, bo.getUserId());
        lqw.eq(bo.getReadTime() != null, SysNoticeRead::getReadTime, bo.getReadTime());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysNoticeReadAddBo bo) {
        SysNoticeRead add = BeanUtil.toBean(bo, SysNoticeRead.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysNoticeReadEditBo bo) {
        SysNoticeRead update = BeanUtil.toBean(bo, SysNoticeRead.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysNoticeRead entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importSysNoticeRead(List<SysNoticeReadVo> sysNoticeReadList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(sysNoticeReadList), "导入数据不能为空");
        int successNum = 0;
        for (SysNoticeReadVo sysNoticeRead : sysNoticeReadList) {
            SysNoticeRead add = BeanUtil.toBean(sysNoticeRead, SysNoticeRead.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", sysNoticeReadList.size(), successNum);
        if (successNum != sysNoticeReadList.size()) {
            msg = msg + String.format("失败 %d 条！", sysNoticeReadList.size() - successNum);
        }
        return msg;
    }
}
